#-------------------------------------------------------------------------------
# Name:        Sound
# Purpose:
#
# Author:      Pablo
#
# Created:     18/04/2012
# Copyright:   (c) Monxcleyr Productions 2012
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os, config, logging

class effect():

  """Class effect()

  Plays a sound effect."""

  def __init__(self, configfile, debug):
    self.debug = debug
    conf = config.Config()
    # Load up ini file
    conf.read(os.path.join('resources/configs/sounds.ini'))

    for option in conf.options('Effects'):
      filename = conf.get('Effects', option)
      tempsound = pygame.mixer.Sound('resources/sounds/effects/' + filename)
      filename2 = 'resources/sounds/effects/' + filename
      setattr(self, option, [tempsound, filename2])

    logging.info('Sound system initialized')

  def play(self, sound, settings):

    if sound != None:

      temp = getattr(self, sound)
      tempsound = pygame.mixer.Sound(temp[1])

      sound_volume = settings.sound_volume * .01
      if settings.sounds == True:
        tempsound.set_volume(sound_volume)
      else:
        tempsound.set_volume(0)

      self.debug.add('Playing ' + temp[1])
      logging.info('Playing sound %s', temp[1])
      tempsound.play()



#-------------------------------------------------------------------------------


class music():

  """Class music()

  Plays background musics. """

  def __init__(self, configfile, debug):
    self.debug = debug
    self.conf = config.Config()
    self.currentloop = None
    # Load up ini file
    self.conf.read(os.path.join('resources/configs/sounds.ini'))

    for option in self.conf.options('Musics'):
      filename = self.conf.get('Musics', option)
      setattr(self, 'track_' + option, ['resources/sounds/music/' + filename, filename])


    self.loop = []

  def play(self, sound, loop):

    if sound != None:

      # Loop is number of loops. -1 = infinite
      pygame.mixer.music.load(getattr(self, 'track_' + sound)[0])
      pygame.mixer.music.play(loop)

      self.debug.add('Playing ' + getattr(self, 'track_' + sound)[1])
      logging.info('Playing music %s', getattr(self, 'track_' + sound)[1])

  def loadQueue(self, queue):

    self.currentloop = queue
    if queue != None:
      self.loop = []

      temp = self.conf.getstr_list('Music_Loops', queue)

      for track in temp:
        self.loop.append(getattr(self, 'track_' + track))

      self.loopcount = -1

      self.debug.add('Loaded music queue "' + queue + '"')
      logging.info('Loaded music loop %s', queue)

    else:
      logging.warning('Failed to load music loop')
      self.loop = []

  def playQueue(self):

    if self.loop != []:

      self.loopcount += 1
      if self.loopcount >= len(self.loop):
        self.loopcount = 0

      currenttrack = self.loop[self.loopcount][0]
      currentfile = self.loop[self.loopcount][1]

      pygame.mixer.music.stop()
      pygame.mixer.music.load(currenttrack)
      self.debug.add('Playing ' + currentfile)
      logging.info('Playing music %s', currentfile)
      pygame.mixer.music.play()

  def getCurrentLoop(self):
    """ Returns currently playing music loop name """
    return self.currentloop
